<?php

require_once 'BlogpostsHandler.php';
require_once 'BlogpostreadView.php';
require_once 'LoginHandler.php';

class BlogpostreadController{
	
	private $ret = "";
	private $message = "";
	
	
	public function DoControll(Database $db){
		$blogpostHandler = new BlogpostsHandler($db);
		$blogpostreadView = new BlogpostreadView();
		$loginHandler = new LoginHandler($db);
		
		
		if($blogpostreadView->TriedToDeletePost()){
				if($blogpostHandler->DeleteBlogpost($blogpostreadView->DeletePostid())){
					$this->message .=$blogpostreadView->Message(BlogpostreadView::DELETED);
					
				}
			}
		$blogposts = $blogpostHandler->GetAllBlogposts();
		
		if($loginHandler->UserIsAdmin() === true){
				$this->ret = $blogpostreadView->ViewBlogpostsAsAdmin($blogposts);
		}
		else
		{
			$this->ret = $blogpostreadView->ViewBlogposts($blogposts);
		}

		//Hämtar formuläret som ska visa det som finns i databasen.
		
		
		return $this->ret. $this->message; 
	}
}
